# coding:utf-8
import tensorflow as tf
import tensorflow.feature_column as fc

import os
import sys

import matplotlib.pyplot as plt
from IPython.display import clear_output

tf.enable_eager_execution()

models_path = os.path.join(r'F:\Github', 'models')
print(sys.path)
sys.path.append(models_path)

from official.wide_deep import census_dataset
from official.wide_deep import census_main
# census_dataset.download("/tmp/census_data/")
train_file = r'E:\tmp\census_data\adult.data'
test_file = r'E:\tmp\census_data\adult.test'
import pandas as pd
train_df = pd.read_csv(train_file,header=None,names = census_dataset._CSV_COLUMNS)
test_df = pd.read_csv(test_file, header = None, names = census_dataset._CSV_COLUMNS)
print(train_df.head())

# FAQ:数据转换为张量
def easy_input_function(df, label_key, num_epochs, shuffle, batch_size):
    label = df[label_key]
    ds = tf.data.Dataset.from_tensor_slices((dict(df),label))
    print('ds',type(ds),dir(ds))
    if shuffle:
        ds = ds.shuffle(10000)

    ds = ds.batch(batch_size).repeat(num_epochs)

    return ds


ds = easy_input_function(train_df, label_key='income_bracket', num_epochs=5, shuffle=True, batch_size=10)
print(ds)
for feature_batch, label_batch in ds.take(1):
    print('Some feature keys:', list(feature_batch.keys())[:5])
    print()
    print('A batch of Ages  :', feature_batch['age'])
    print()
    print('A batch of Labels:', label_batch )
# import inspect
# # REW:等效上个
# # print(inspect.getsource(census_dataset.input_fn))
# ds = census_dataset.input_fn(train_file, num_epochs=5, shuffle=True, batch_size=10)
#
# for feature_batch, label_batch in ds.take(1):
#     print('Feature keys:', list(feature_batch.keys())[:5])
#     print()
#     print('Age batch   :', feature_batch['age'])
#     print()
#     print('Label batch :', label_batch )
#
# import functools
# # 因为 Estimators 要求 input_fn 不接受任何参数，因此我们通常会将可配置的输入函数封装到带预期签名的对象中。对于此笔记本，请配置 train_inpf 以迭代数据两次
# train_inpf = functools.partial(census_dataset.input_fn, train_file, num_epochs=2, shuffle=True, batch_size=64)
# test_inpf = functools.partial(census_dataset.input_fn, test_file, num_epochs=1, shuffle=False, batch_size=64)